package com.ruoyi.market.service.impl;

import com.ruoyi.common.constant.DictConstants;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanValidators;
import com.ruoyi.market.domain.Coupon;
import com.ruoyi.market.mapper.CouponMapper;
import com.ruoyi.market.service.ICouponService;
import com.ruoyi.system.service.ISysConfigService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.validation.Validator;
import java.util.List;

/**
 * 卡券 业务层处理
 * 
 * @author ruoyi
 */
@Service
public class CouponServiceImpl implements ICouponService
{
    private static final Logger log = LoggerFactory.getLogger(CouponServiceImpl.class);

    @Autowired
    private CouponMapper couponMapper;

    @Autowired
    private ISysConfigService configService;

    @Autowired
    protected Validator validator;

    /**
     * 根据条件分页查询卡券列表
     * 
     * @param coupon 卡券信息
     * @return 卡券信息集合信息
     */
    @Override
    public List<Coupon> selectCouponList(Coupon coupon)
    {
        return couponMapper.selectCouponList(coupon);
    }

    /**
     * 通过卡券名查询卡券
     * 
     * @param couponTitle 卡券名
     * @return 卡券对象信息
     */
    @Override
    public Coupon selectCouponByCouponTitle(String couponTitle)
    {
        return couponMapper.selectCouponByCouponTitle(couponTitle);
    }

    /**
     * 通过卡券ID查询卡券
     * 
     * @param couponId 卡券ID
     * @return 卡券对象信息
     */
    @Override
    public Coupon selectCouponById(Long couponId)
    {
        return couponMapper.selectCouponById(couponId);
    }

    /**
     * 校验卡券是否唯一
     *
     * @param coupon 卡券信息
     * @return 结果
     */
    @Override
    public boolean checkCouponUnique(Coupon coupon)
    {
        Long couponId = StringUtils.isNull(coupon.getCouponId()) ? -1L : coupon.getCouponId();
        Coupon info = couponMapper.checkCouponUnique(coupon);
        if (StringUtils.isNotNull(info) && info.getCouponId().longValue() != couponId.longValue())
        {
            return DictConstants.NOT_UNIQUE;
        }
        return DictConstants.UNIQUE;
    }

    /**
     * 新增保存卡券信息
     * 
     * @param coupon 卡券信息
     * @return 结果
     */
    @Override
    @Transactional
    public int insertCoupon(Coupon coupon)
    {
        // 新增卡券信息
        int rows = couponMapper.insertCoupon(coupon);
        return rows;
    }

    /**
     * 修改保存卡券信息
     * 
     * @param coupon 卡券信息
     * @return 结果
     */
    @Override
    @Transactional
    public int updateCoupon(Coupon coupon)
    {
        return couponMapper.updateCoupon(coupon);
    }

    /**
     * 修改卡券状态
     * 
     * @param coupon 卡券信息
     * @return 结果
     */
    @Override
    public int updateCouponStatus(Coupon coupon)
    {
        return couponMapper.updateCoupon(coupon);
    }

    /**
     * 批量删除卡券信息
     * 
     * @param couponIds 需要删除的卡券ID
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteCouponByIds(Long[] couponIds) { return couponMapper.deleteCouponByIds(couponIds); }

    /**
     * 导入卡券数据
     * 
     * @param couponList 卡券数据列表
     * @param isUpdateSupport 是否更新支持，如果已存在，则进行更新数据
     * @param operName 操作卡券
     * @return 结果
     */
    @Override
    public String importCoupon(List<Coupon> couponList, Boolean isUpdateSupport, String operName)
    {
        if (StringUtils.isNull(couponList) || couponList.size() == 0)
        {
            throw new ServiceException("导入卡券数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (Coupon coupon : couponList)
        {
            try
            {
                // 验证是否存在这个卡券
                Coupon u = couponMapper.checkCouponUnique(coupon);
                if (StringUtils.isNull(u))
                {
                    BeanValidators.validateWithException(validator, coupon);
                    coupon.setCreateBy(operName);
                    couponMapper.insertCoupon(coupon);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、卡券 " + coupon.getCouponTitle() + " 导入成功");
                }
                else if (isUpdateSupport)
                {
                    BeanValidators.validateWithException(validator, coupon);
                    coupon.setCouponId(u.getCouponId());
                    coupon.setUpdateBy(operName);
                    couponMapper.updateCoupon(coupon);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、卡券 " + coupon.getCouponTitle() + " 更新成功");
                }
                else
                {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、卡券 " + coupon.getCouponTitle() + " 已存在");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、卡券 " + coupon.getCouponTitle() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
